export const array = {
    //生成多个数组
    moreNewArray(list,num){
        let arrList = [];
        let newArr = [];
        let numList = [];
        let n = 0;
        if(list.length > num){
            list.forEach((item,index)=>{
                if( index % num === 0 ){
                    numList.push(index);
                }
            })
            numList.forEach((item,index)=>{
                newArr[n++] = list.slice(item, item+num);
                arrList = newArr;
            })
        }
        return arrList
    },

    //数组排序
    arraySort(arr, sortText) {
        if (!sortText) {
            return arr
        }
        var _sortText = sortText.split(',').reverse(), _arr = arr.slice(0);
        for (var i = 0, len = _sortText.length; i < len; i++) {
            _arr.sort(function (n1, n2) {
                return n1[_sortText[i]] - n2[_sortText[i]]
            })
        }
        return _arr;
    },

    //数组重复提取
    classListExtractRepetition(arr){
        const mapper = {};
        const newArr = [];

        for (let item of arr) {
        	  const { classId } = item;
        	  if (mapper[classId]) {
        			newArr.push(item)
        	  } else {
        			mapper[classId] = true
        	  }
        }
        return newArr;
    },

    subjectListExtractRepetition(arr){
        const mapper = {};
        const newArr = [];

        for (let item of arr) {
        	  const { subjectId } = item;
        	  if (mapper[subjectId]) {
        			newArr.push(item)
        	  } else {
        			mapper[subjectId] = true
        	  }
        }
        return newArr;
    },

    arrRepetitionExtract(arr){
        const mapper = {};
        const newArr = [];

        for (let item of arr) {
        	const { id } = item;
        	  if (mapper[id]) {
        			newArr.push(item)
        	  } else {
        			mapper[id] = true
        	  }
        }
        return newArr;
    },

    //数组去除空对象
    arrclearNotObj(arr){
        for (let i = 0; i < arr.length; i++) {
          if (arr[i] == null || arr[i] == "" || JSON.stringify(arr[i]) == "{}") {
            arr.splice(i, 1);
            i = i - 1;
          }
        }
        return arr
    },



    //数组去重
    removeRepeatArray(list){
        let type = Number;
        list.forEach(item => {
             if( Object.prototype.toString.call(item) === '[object Object]'){
                 type = 1;
             }else{
                 type = 0;
             }
        });
        if(type == 1){
           let uniques = [];
           let stringify = {};
           for (let i = 0; i < list.length; i++) {
               let keys = Object.keys(list[i]);
               keys.sort(function(a, b) {
                   return (Number(a) - Number(b));
               });
               let str = '';
               for (let j = 0; j < keys.length; j++) {
                   str += JSON.stringify(keys[j]);
                   str += JSON.stringify(list[i][keys[j]]);
               }
               if (!stringify.hasOwnProperty(str)) {
                   uniques.push(list[i]);
                   stringify[str] = true;
               }
           }
           uniques = uniques;
           return uniques;
        }else{
           return Array.from(new Set(list))
        }
    }
}



//element 校验数字
export const validNumber  = (rule, value, callback) => {
    if(!Number(value)){
        return callback(new Error('请输入数字'));
    }
    callback();
};

//element 校验手机号码
export const validPhone = (rule, value, callback) => {
  const reg = /^((0\d{2,3}-\d{7,8})|(1[35784]\d{9}))$/;
  if (!value) {
      callback(new Error('请输入手机号码'))
  }else if(!Number(value)){
      callback(new Error('手机号码必须为数字'))
  }else if (!reg.test(value)) {
      callback(new Error('请输入正确的手机号码'))
  } else {
      callback()
  }
}


//数据参数
export const setParams = {
    setUrlPrmt(obj) {
        let _rs = [];
        for (let p in obj) {
            if (obj[p] != null && obj[p] != '') {
                _rs.push(p + '=' + obj[p])
            };
        };
        return _rs.join('&');
    }
}